	/**
	 * AuthWindow.as
	 * =============
	 * Script file for AuthenticationWindow class.
	 * 
	 * @author Janov Byrnisson
	 * @date 13-jul-2009
	 */
	
	import classes.MySQLService;
	import classes.MySqlStatus;
	import classes.events.MySqlErrorEvent;
	import classes.events.MySqlResultEvent;
	import classes.utils.CookieUtil;
	import classes.utils.MD5;
	
	import components.CustomAlert;
	
	import flash.events.MouseEvent;
	
	import mx.managers.PopUpManager;
	
	
	private var incorrectionAlert:CustomAlert;
	
	/**
	 * Click handler of the Connect button.
	 */
	private function mxConnBtn_onClick( e:MouseEvent ):void
	{
		var langChoice:String;
		
		switch( mxGatewayLang.selectedItem.data )
		{
			case MySQLService.PHP_GATEWAY		:	langChoice = MySQLService.PHP_GATEWAY; break;
			case MySQLService.PERL_GATEWAY		:	langChoice = MySQLService.PERL_GATEWAY; break;
			case MySQLService.PYTHON_GATEWAY	:	langChoice = MySQLService.PYTHON_GATEWAY; break;
		}
		
		flashSQL.LANG_PREF = langChoice;
		
		var myService = new MySQLService( langChoice );
		
		myService.authenticate( loginResult, loginFault, mxUsername.text, mxPassword.text );
	}


	private function loginResult( e:MySqlResultEvent ):void
	{
		if( mxRememberMe.selected )
		{
			CookieUtil.setCookie( flashSQL.MD5_HASH, MD5.encrypt( mxUsername.text ), 2 );
		}
		else
		{
			if( CookieUtil.getCookie( flashSQL.MD5_HASH ) )
			{
				CookieUtil.deleteCookie( flashSQL.MD5_HASH );
			}
		}
		
		PopUpManager.removePopUp( this );
		
		flashSQL.mainCont = new MainContainer();
			
		parentApplication.addChild( flashSQL.mainCont );
		
		//var panel:TestPanel = flashSQL.mainCont.mdiFrame.openPanel( TestPanel ) as TestPanel;
		//var panel2:TestPanel = flashSQL.mainCont.mdiFrame.openPanel( TestPanel ) as TestPanel;		
	}
	
	
	private function loginFault( e:MySqlErrorEvent ):void
	{
		incorrectionAlert = PopUpManager.createPopUp( this, CustomAlert, false ) as CustomAlert;
		
		var warningText:String;  
		
		if( e.details == MySqlStatus.DETAILS_BAD_USERNAME )
			warningText = "Incorrect username! Please try again with correct user name.";
		
		if( e.details == MySqlStatus.DETAILS_BAD_PASSWORD )
			warningText = "Incorrect password! Please type the password again.";
			
		incorrectionAlert.title = "Access denied";
		incorrectionAlert.mxAlertText.text = warningText;
		
		PopUpManager.centerPopUp( incorrectionAlert );		
	}